package java6681.newdate;

import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.DatePicker;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.net.URL;
import java.time.LocalDate;
import java.time.MonthDay;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;


public class BirthDay6681Controller {

    @FXML
    private DatePicker dpBirthDay;

    @FXML
    private Label lblResult;

    @FXML
    void change() {
        LocalDate birthDay = dpBirthDay.getValue();
        LocalDate today = LocalDate.now();
        if (birthDay == null) {
            lblResult.setText("请输入日期");
        } else if (birthDay.isAfter(today)) {
            lblResult.setText("还没出生哦！");
        } else {
            /*今年的生日*/
            LocalDate newBirthDay = birthDay.with(ChronoField.YEAR, today.getYear());
            /*今年生日的时间间隔=今年生日-今天的日期*/
            long nDay = today.until(newBirthDay, ChronoUnit.DAYS);
            /*计算年龄*/
            long nYear = birthDay.until(today, ChronoUnit.YEARS);
            String strResult = null;
            if (nYear == 0) {
                strResult = "刚出生";
            } else if (nDay == 0) {
                strResult = nYear + "岁，生日快乐";
            } else if (nDay > 0) {
                strResult = "还有" + nDay + "天，" + (nYear + 1) + "岁生日，有惊喜";
            } else {
                strResult = nYear + "岁生日已过，等待来年！";
            }
            lblResult.setText(strResult);
        }
    }
}
